package cn.wit.summit.chwan.ui.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.adapters.FragmentPagerAdapter;
import cn.wit.summit.chwan.models.event.DownloadEvent;
import cn.wit.summit.chwan.ui.base.BaseActivity;
import cn.wit.summit.chwan.ui.fragment.AllNetHotGameFragment;
import cn.wit.summit.chwan.utils.StringUtils;

/**
 * Created by Alan on 2018/7/18.
 */

public class AllNetHotGameActivity extends BaseActivity {

    @Bind(R.id.allNetYQWIv)
    ImageView allNetYQWIv;
    @Bind(R.id.allNetYQWTv)
    TextView allNetYQWTv;
    @Bind(R.id.allNetYQWLl)
    LinearLayout allNetYQWLl;
    @Bind(R.id.allNetYYBIv)
    ImageView allNetYYBIv;
    @Bind(R.id.allNetYYBTv)
    TextView allNetYYBTv;
    @Bind(R.id.allNetYYBLl)
    LinearLayout allNetYYBLl;
    @Bind(R.id.allNetGOOGLEIv)
    ImageView allNetGOOGLEIv;
    @Bind(R.id.allNetGOOGLETv)
    TextView allNetGOOGLETv;
    @Bind(R.id.allNetGOOGLELl)
    LinearLayout allNetGOOGLELl;
    @Bind(R.id.allNetTTIv)
    ImageView allNetTTIv;
    @Bind(R.id.allNetTTTv)
    TextView allNetTTTv;
    @Bind(R.id.allNetTTLl)
    LinearLayout allNetTTLl;
    @Bind(R.id.allNetViewPager)
    ViewPager allNetViewPager;
    @Bind(R.id.allHotGameRefreshTimeTv)
    TextView allHotGameRefreshTimeTv;

    private List<LinearLayout> topLl = new ArrayList<>();
    private List<TextView> topTv = new ArrayList<>();
    private List<ImageView> topIv = new ArrayList<>();
    private List<Integer> seletedIv = new ArrayList<>();
    private List<Integer> unseletIv = new ArrayList<>();
    private String[] titleTop = {"yqw", "yyb", "google", "taptap"};

    private List<FragmentPagerAdapter.Item> items = new ArrayList<>();
    private int mPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_net_hot_game);
        ButterKnife.bind(this);

        String po = getIntent().getStringExtra("mPosition");
        if (StringUtils.isEmpty(po)) {
            mPosition = 0;
        } else {
            if ("0".equals(po))
                mPosition = 0;
            else if ("1".equals(po))
                mPosition = 1;
            else if ("2".equals(po))
                mPosition = 2;
            else if ("3".equals(po))
                mPosition = 3;
            else
                mPosition = 0;
        }
        setBarTitleTx(R.string.all_net_title_str);

        initTopTab();
        initViewPager();
    }

    public void setAllHotGameRefreshTimeTv(String allHotGameRefreshTimeTv) {
        this.allHotGameRefreshTimeTv.setText("最后更新时间："+allHotGameRefreshTimeTv);
    }

    private void initViewPager() {
        items.clear();
        for (int i = 0; i < 4; i++) {
            AllNetHotGameFragment tempFragment = AllNetHotGameFragment.newInstance(i, i==mPosition);
            items.add(new FragmentPagerAdapter.Item(titleTop[i], tempFragment));
        }
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager(), items);
        allNetViewPager.setAdapter(adapter);
        allNetViewPager.setOffscreenPageLimit(4);
        allNetViewPager.setCurrentItem(mPosition);
        seleteTopTab(mPosition);
        allNetViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                seleteTopTab(position);
                mPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void initTopTab() {
        topLl.add(allNetYQWLl);
        topLl.add(allNetYYBLl);
        topLl.add(allNetGOOGLELl);
        topLl.add(allNetTTLl);

        topTv.add(allNetYQWTv);
        topTv.add(allNetYYBTv);
        topTv.add(allNetGOOGLETv);
        topTv.add(allNetTTTv);

        topIv.add(allNetYQWIv);
        topIv.add(allNetYYBIv);
        topIv.add(allNetGOOGLEIv);
        topIv.add(allNetTTIv);

        unseletIv.add(R.mipmap.top_yqw_unselect);
        unseletIv.add(R.mipmap.top_yyb_unselect);
        unseletIv.add(R.mipmap.top_google_unselect);
        unseletIv.add(R.mipmap.top_tt_unselect);

        seletedIv.add(R.mipmap.top_yqw_selected);
        seletedIv.add(R.mipmap.top_yyb_selected);
        seletedIv.add(R.mipmap.top_google_selected);
        seletedIv.add(R.mipmap.top_tt_selected);
    }

    @OnClick({R.id.allNetYQWLl, R.id.allNetYYBLl, R.id.allNetGOOGLELl, R.id.allNetTTLl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.allNetYQWLl:
                seleteTopTab(0);
                break;
            case R.id.allNetYYBLl:
                seleteTopTab(1);
                break;
            case R.id.allNetGOOGLELl:
                seleteTopTab(2);
                break;
            case R.id.allNetTTLl:
                seleteTopTab(3);
                break;
        }
    }

    private void seleteTopTab(int po) {
        allNetViewPager.setCurrentItem(po);
        for (int i = 0; i < 4; i++) {
            if (i == po) {
                topLl.get(i).setBackgroundResource(R.drawable.hotgame_bar_selected_bg);
                topTv.get(i).setTextColor(getResources().getColor(R.color.color_white));
//                topTv.get(i).setTextColor(getResources().getColor(R.color.color_2681FB));
                topIv.get(i).setImageResource(seletedIv.get(i));
            } else {
                topLl.get(i).setBackgroundResource(R.drawable.hotgame_bar_unselect_bg);
                topTv.get(i).setTextColor(getResources().getColor(R.color.color_black));
                topIv.get(i).setImageResource(unseletIv.get(i));
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(DownloadEvent downloadEvent) {
        recommendTopDownload.setTotalDownloadNum();//设置下载数
        for (int i = 0; items != null && i < items.size(); i++) {
            if (i==mPosition) {
                ((AllNetHotGameFragment) items.get(i).fragment).changeDownloadStatus(downloadEvent);
            }
        }
    }

    @Override
    public void reGetData() {
        super.reGetData();
        initViewPager();
    }
}
